import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import store from "@/store";
import Layout from "@/views/layout/index.vue";
const routes: Array<RouteRecordRaw> = [
  {
    path: "/login",
    name: "login",
    component: () =>
      import(/* webpackChunkName:'login' */ "@/views/login/index.vue"),
  },
  {
    path: "/:catchAll(.*)",
    name: "404",
    component: () =>
      import(/* webpackChunkName:'404' */ "@/views/error-page/404.vue"),
  },
  {
    path: "/",
    component: Layout,
    meta: {
      requiresAuth: true,
    },
    children: [
      {
        path: "",
        name: "home",
        component: () =>
          import(/* webpackChunkName:'home' */ "@/views/home/index.vue"),
      },
      {
        path: "/role",
        name: "role",
        component: () =>
          import(/* webpackChunkName:'role' */ "@/views/role/index.vue"),
      },
      {
        path: "/menu",
        name: "menu",
        component: () =>
          import(/* webpackChunkName:'role' */ "@/views/menu/index.vue"),
      },
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    if (!store.state.user) {
      next({ path: "/login", query: { redirect: to.fullPath } });
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router;
